﻿// MainDlg.cpp: 实现文件
//

#include "pch.h"
#include "wowlua02.h"
#include "mgr.h"
#include "lua_register.h"


// MainDlg 对话框

IMPLEMENT_DYNAMIC(MainDlg, CDialogEx)

MainDlg::MainDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_MAIN, pParent)
{

}

MainDlg::~MainDlg()
{
}

void MainDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_TAB1, tab_);
}


BEGIN_MESSAGE_MAP(MainDlg, CDialogEx)
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_BUTTON1, &MainDlg::OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BUTTON2, &MainDlg::OnBnClickedButton2)
	ON_BN_CLICKED(IDC_BUTTON3, &MainDlg::OnBnClickedButton3)
END_MESSAGE_MAP()


// MainDlg 消息处理程序


BOOL MainDlg::OnInitDialog() {
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	tab_.AddPage("Page1", &page1_, IDD_PAGE1);
	tab_.AddPage("Page2", &page2_, IDD_PAGE2);
	tab_.Show();

	HWND hwnd_game = get_main_wnd("GxWindowClass");
	uint32 pid = GetCurrentProcessId();
	uint64 module_base = get_module_base(pid).value_or(0);
	dbgprint("codergege [%s][%03d] 窗口句柄[0x%X]", __FUNCTION__, __LINE__, hwnd_game);
	dbgprint("codergege [%s][%03d] 游戏基址[0x%llX]", __FUNCTION__, __LINE__, module_base);

	Mgr::get()->init(pid, hwnd_game, module_base);
	Mgr::get()->game()->call()->attach_main_thread();

	//dbgprint("codergege 注册LUA接口开始 [%s][%d]", __FUNCTION__, __LINE__);
	//lua_reg();
	//Mgr::get()->engine()->bypass_lua_invoke_checking();
	//dbgprint("codergege 注册LUA接口结束 [%s][%d]", __FUNCTION__, __LINE__);

	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常: OCX 属性页应返回 FALSE
}


void MainDlg::OnClose() {
    Mgr::get()->game()->call()->detach_main_thread();
	CDialogEx::OnClose();
}


// 数据定位
#include <fstream>
void MainDlg::OnBnClickedButton1() {
	std::string data = Mgr::get()->feature_code_positioning();
	const std::string& file_string("rva.h");
	std::ofstream OsWrite(file_string, std::ofstream::app);
	OsWrite << data;
	OsWrite << std::endl;
	OsWrite.close();
}


void MainDlg::OnBnClickedButton2() {
	if (AllocConsole()) {
		FILE* file = 0;
		freopen_s(&file, "CONOUT$", "w+t", stdout);
	} else {
		if (::IsWindowVisible(GetConsoleWindow())) {
			::ShowWindow(GetConsoleWindow(), SW_HIDE);
		} else {
			::ShowWindow(GetConsoleWindow(), SW_SHOW);
		}
	}
}


void MainDlg::OnBnClickedButton3() {
    FreeConsole();
}
